package com.lianniu.mgr.projectMgr.projectInfo.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.serialNo.model.SerialNoVo;
import com.lianniu.mgr.baseService.serialNo.service.ISerialNoService;
import com.lianniu.mgr.baseService.staff.model.StaffVo;
import com.lianniu.mgr.baseService.staff.service.IStaffService;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.projectMgr.projectInfo.model.ProjectStaffVo;
import com.lianniu.mgr.projectMgr.projectInfo.service.IProjectStaffService;
import com.lianniu.mgr.util.FileUtil;
import com.lianniu.mgr.util.StringUtil;

@Controller
@RequestMapping("/rest/projectStaff")
public class ProjectStaffRestController {	
	private final static Logger logger = Logger.getLogger(ProjectStaffRestController.class);
	
	@Autowired
	private IProjectStaffService projectStaffService;
	
	@Autowired
	private ISerialNoService serialNoService;
	
	@Autowired
	private IStaffService staffService;
		
	/**
	  * @Description:获取项目人员的分页列表信息
      * @return BaseResult(data=List<ProjectStaffVo>)

	  * @Date:2016年3月24日 下午4:23:24
	  * @Version 1.0.0
	  */
	@RequestMapping(value = {"/list"}, method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody BaseResult list(HttpServletRequest request, ProjectStaffVo projectStaff, PaginationInfo paginationInfo) {
		logger.info("ProjectStaffRestController list begin.");
		logger.info("ProjectStaffRestController list projectStaff:"+projectStaff);
		projectStaff.setPaginationInfo(paginationInfo);
		logger.info("projectStaffService getProjectStaffsByVoForPage begin.");
		BaseResult baseResult = projectStaffService.getProjectStaffsByVoForPage(projectStaff);
		logger.info("projectStaffService getProjectStaffsByVoForPage end.");
		logger.info("ProjectStaffRestController list end.");
		return baseResult;
	}
	
	/**
	  * @Description:添加项目人员记录
      * @return BaseResult

	  * @Date:2016年3月24日 下午4:23:24
	  * @Version 1.0.0
	  */
	@RequestMapping(value = {"/add"}, method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody BaseResult add(HttpServletRequest request, ProjectStaffVo projectStaff) {
		logger.info("ProjectStaffRestController add begin.");
		projectStaff.setId(StringUtil.getUUID());
		projectStaff.setDelFlag("1");
		logger.info("ProjectStaffRestController add projectStaff:"+projectStaff);
		logger.info("projectStaffService addProjectStaff begin.");
		BaseResult baseResult = projectStaffService.addProjectStaff(projectStaff);
		logger.info("projectStaffService addProjectStaff end.");
		logger.info("ProjectStaffRestController add end.");
		return baseResult;
	}
	
	/**
	  * @Description:更新项目人员记录信息
      * @return BaseResult

	  * @Date:2016年3月24日 下午4:23:24
	  * @Version 1.0.0
	  */
	@RequestMapping(value = {"/edit"}, method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody BaseResult edit(HttpServletRequest request, ProjectStaffVo projectStaff) {
		logger.info("ProjectStaffRestController edit begin.");
		projectStaff.setDelFlag("1");
		logger.info("ProjectStaffRestController edit projectStaff:"+projectStaff);
		logger.info("projectStaffService editProjectStaffById begin.");
		BaseResult baseResult = projectStaffService.editProjectStaffById(projectStaff);
		logger.info("projectStaffService editProjectStaffById end.");
		logger.info("ProjectStaffRestController edit end.");
		return baseResult;
	}
	
	/**
	  * @Description:批量删除项目人员记录信息
      * @return BaseResult

	  * @Date:2016年3月24日 下午4:23:24
	  * @Version 1.0.0
	  */
	@RequestMapping(value = {"/delete"}, method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody BaseResult delete(HttpServletRequest request) {
		logger.info("ProjectStaffRestController list begin.");
		String id = request.getParameter("ids");
		logger.info("ProjectStaffRestController delete id:"+id);
		List<String> ids = new ArrayList<String>();
		if(!StringUtil.isEmpty(id)){
			String[] idIns = id.split(",");
			for(String idIn : idIns){
				ids.add(idIn);
			}
		}
		logger.info("ProjectStaffRestController delProjectStaffsByIds ids:"+ids);
		logger.info("projectStaffService delProjectStaffsByIds begin.");
		BaseResult baseResult = projectStaffService.delProjectStaffsByIds(ids);
		logger.info("projectStaffService delProjectStaffsByIds end.");
		logger.info("ProjectStaffRestController delete end.");
		return baseResult;
	}
	
	/**
	  * @Description:根据项目id查询负责人列表
      * @return BaseResult(data=List<ProjectStaffVo>)

	  * @Date:2016年3月24日 下午4:23:24
	  * @Version 1.0.0
	  */
	@RequestMapping(value = {"/listStaff"}, method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody BaseResult listStaff(HttpServletRequest request) {
		logger.info("ProjectStaffRestController listStaff begin.");
		String projectId = request.getParameter("projectId");
		logger.info("ProjectStaffRestController list projectId:"+projectId);
		ProjectStaffVo projectStaff = new ProjectStaffVo();
		projectStaff.setProjectId(projectId);
		// 根据  PostName like "%负责人%" 区分 项目负责人还是其他人员
		projectStaff.setPostName("负责人");
		logger.info("projectStaffService getProjectStaffsByVo begin.");
		BaseResult baseResult = projectStaffService.getProjectStaffsByVo(projectStaff);
		logger.info("projectStaffService getHeadsByProjectId end.");
		logger.info("ProjectStaffRestController listStaff end.");
		return baseResult;
	}
	
	/**
	  * @Description:获取项目人员记录信息
      * @return BaseResult

	  * @Date:2016年3月24日 下午4:23:24
	  * @Version 1.0.0
	  */
	@RequestMapping(value = {"/get"}, method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody BaseResult get(HttpServletRequest request, ProjectStaffVo projectStaff) {
		logger.info("ProjectStaffRestController get begin.");
		logger.info("ProjectStaffRestController get projectStaff:"+projectStaff);
		logger.info("projectStaffService getProjectStaffsByVo begin.");
		BaseResult baseResult = projectStaffService.getProjectStaffsByVo(projectStaff);
		logger.info("projectStaffService getProjectStaffsByVo end.");
		logger.info("ProjectStaffRestController get end.");
		return baseResult;
	}
	
	/**
	  * @Description:获取内部人员详细详细信息列表
      * @return BaseResult(data=List<StaffVo>)

	  * @Date:2016年3月24日 下午4:23:24
	  * @Version 1.0.0
	  */
	@RequestMapping(value = {"/seleteStaff"}, method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody BaseResult seleteStaff(HttpServletRequest request) {
		logger.info("ProjectStaffRestController seleteStaff begin.");
		logger.info("projectStaffService getStaff begin.");
		BaseResult baseResult = projectStaffService.getStaff(new StaffVo());
		logger.info("projectStaffService getStaff end.");
		logger.info("ProjectStaffRestController seleteStaff end.");
		return baseResult;
	}
	
	/**
	  * @Description:获取项目人员编号
      * @return BaseResult

	  * @Date:2016年3月24日 下午4:23:24
	  * @Version 1.0.0
	  */
	@RequestMapping(value = {"/getProjectStaffNo"}, method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody BaseResult getProjectStaffNo(HttpServletRequest request) {
		logger.info("ProjectRestController getProjectStaffNo begin.");
		String userName = request.getParameter("userName");
		logger.info("ProjectRestController getProjectStaffNo userName:"+userName);
		logger.info("serialNoService getSerialNoId begin.");
		BaseResult baseResult = serialNoService.getSerialNoId(userName);
		logger.info("serialNoService getSerialNoId end.");
		logger.info("ProjectRestController getProjectStaffNo end.");
		return baseResult;
	}
	
	/**
	  * @Description:导入项目人员
     * @return BaseResult

	  * @Date:2016年3月24日 下午4:23:24
	  * @Version 1.0.0
	  */
	@RequestMapping(value = {"/importStaff"}, method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody BaseResult importStaff(HttpServletRequest request, MultipartFile file) throws Exception {
		logger.info("ProjectRestController importStaff begin.");
		List<Map<String, String>> list = FileUtil.getXlsInfo(file.getInputStream(), 2);
		logger.info("ProjectRestController importStaff list:"+list);
		if(list.size() < 1){
			return BaseResult.success();
		}
		// 读取文档内容，保存到数据库中
		List<ProjectStaffVo> listVo = new ArrayList<>();
		ProjectStaffVo projectStaffVo;
		String projectStaffId;
		BaseResult serialNo;
		SerialNoVo serialNoVo;
		String addUser = request.getParameter("addUser");
		String projectId = request.getParameter("projectId");
		String staffName;
		String staffType;
		StaffVo staffVo;
		// 失败记录数
		int falseNum = 0;
		for(Map<String, String> map : list){
			serialNo = serialNoService.getSerialNoId(addUser);
			staffType = map.get("人员类型");
			staffName = map.get("姓名");
			// 根据姓名查询内部员工信息
			serialNoVo = (SerialNoVo)serialNo.getData();
			projectStaffVo = new ProjectStaffVo();
			// 内部人员
			if("1".equals(staffType)){
				staffVo = new StaffVo();
				staffVo.setStaffName(staffName);
				staffVo = (StaffVo)staffService.getStaff(staffVo).getData();
				if(staffVo != null){
					projectStaffVo.setStaffId(staffVo.getStaffId());
				}else{
					falseNum++;
					continue;
				}
			}
			projectStaffId = StringUtil.getUUID();
			projectStaffVo.setId(projectStaffId);
			projectStaffVo.setProjectId(projectId);
			projectStaffVo.setStaffName(staffName);
			projectStaffVo.setPostName(map.get("项目岗位名称"));
			projectStaffVo.setStaffType(staffType);
			projectStaffVo.setDelFlag("1");
			projectStaffVo.setAddUser(addUser);
			projectStaffVo.setSex(map.get("性别"));
			projectStaffVo.setEmail(map.get("邮箱"));
			projectStaffVo.setPhone(map.get("手机"));
			projectStaffVo.setCompanyName(map.get("公司名称"));
			projectStaffVo.setPostType(map.get("项目岗位类型"));
			projectStaffVo.setProjectStaffNo(serialNoVo.getId());
			listVo.add(projectStaffVo);
		}
		logger.info("projectStaffService addProjectStaffs begin.");
		BaseResult baseResult = projectStaffService.addProjectStaffs(listVo);
		if(falseNum > 0){
			baseResult.setRetCode("0001");
			baseResult.setRetMsg(falseNum+"条记录导入失败！");
		}
		logger.info("projectStaffService addProjectStaffs end.");
		logger.info("ProjectRestController importStaff end.");
		return baseResult;
	}
	
}
